import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JWindow;

import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.SwingUtilities;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class FrameEstudiante extends javax.swing.JFrame {
	private JPanel jPanel1;
	private JLabel jLabel1;
	private JButton BTBuscar;
	private JWindow jWindow1;
	private JTextField TxtApellido;
	private JLabel jLabel8;
	private JLabel jLabel7;
	private JTextField TxtNombre;
	private JTextField TxtCedula;
	private JLabel jLabel6;
	private JButton BTCancelar;
	private JButton BTEliminar;
	private JButton BTIncluir;
	private JButton BTBorrar;
	private Seccion sec;

	public FrameEstudiante(Seccion sec) {
		super();
		this.sec = sec;
		initGUI();
	}
	public FrameEstudiante() {
		super();
		initGUI();
	}
	ActionListener AccBuscar = new ActionListener()
	{
		public void actionPerformed(ActionEvent e)
		{
			Buscar();
		}
    };
    public void Buscar()
    {
    	if(this.TxtCedula.getText().equals(""))
    		JOptionPane.showMessageDialog(null,"Debe ingresar una cedula", "Error", JOptionPane.ERROR_MESSAGE);
    	else
    	{
    		try
    		{
		    	int x = this.sec.Buscar((long)Integer.parseInt(TxtCedula.getText()));
		        if(x == -1)
		        {
		        	this.BTIncluir.setEnabled(true);
		        	this.BTEliminar.setEnabled(false);
		    		this.BTBuscar.setEnabled(false);
		    		this.BTBorrar.setEnabled(true);
		    		this.TxtCedula.setEditable(false);
		    		this.TxtApellido.setEditable(true);
		    		this.TxtNombre.setEditable(true);
		    		this.TxtNombre.setFocusable(true);
		        }
		        else
		        {
		        	Estudiante e = this.sec.getEstudiante(x);
		        	TxtCedula.setText(Long.toString(this.sec.getEstudiante(x).getCedula()));
		    		TxtNombre.setText(e.getNombre());
		    		TxtApellido.setText(e.getApellido());
		    		this.BTIncluir.setEnabled(false);
		    		this.BTBuscar.setEnabled(false);
		    		this.BTBorrar.setEnabled(true);
		    		this.BTEliminar.setEnabled(true);
		    		
		    		this.TxtCedula.setEditable(false);
		    		this.TxtApellido.setEditable(true);
		    		this.TxtNombre.setEditable(true);
		        }
    		}catch(Exception e)
    		{
    		  	JOptionPane.showMessageDialog(null,"Ingrese datos validos", "Error", JOptionPane.ERROR_MESSAGE);
    		  	Borrar();
    		}
    	}
    }
    
	ActionListener Incluir = new ActionListener()
	{
		public void actionPerformed(ActionEvent e)
		{
			IncluirEst();
		}
    };
    public void IncluirEst()
    {
    	try
		{
	    	if(this.TxtApellido.getText().equals("") || this.TxtNombre.getText().equals(""))
	    		JOptionPane.showMessageDialog(null,"Debe llenar todos los datos para poder inscribir un estudiante", "Faltan Datos", JOptionPane.ERROR_MESSAGE);
	    	else
	    	{
		    	Estudiante e = new Estudiante((long)Integer.parseInt(TxtCedula.getText()),
		    	TxtNombre.getText(),TxtApellido.getText());
		    	this.sec.InscribirEstudiantes(e);
		    	JOptionPane.showMessageDialog(null,"El Estudiante fué incluido con éxito", "Operación Realizada", JOptionPane.INFORMATION_MESSAGE);
		    	Borrar();
	    	}
		}catch(Exception e)
		{
		  	JOptionPane.showMessageDialog(null,"Ingrese datos validos", "Error", JOptionPane.ERROR_MESSAGE);
		  	Borrar();
		}
    }
    
	ActionListener Borrar = new ActionListener()
	{
		public void actionPerformed(ActionEvent e)
		{
			BTBorrar.setMnemonic(KeyEvent.VK_B);
			Borrar();
		}
    };
    
	public void Borrar()
	{
		TxtCedula.setText("");
		TxtNombre.setText("");
		TxtApellido.setText("");
		this.BTIncluir.setEnabled(false);
		this.BTBuscar.setEnabled(true);
		this.BTBorrar.setEnabled(true);
		this.BTEliminar.setEnabled(false);
		this.TxtCedula.setEditable(true);
		this.TxtApellido.setEditable(false);
		this.TxtNombre.setEditable(false);
	}
	
	ActionListener Eliminar = new ActionListener()
	{
		public void actionPerformed(ActionEvent e)
		{
				Elimina();
		}
    };
	public void Elimina()
	{
		this.sec.RetirarEstudiante(this.sec.Buscar((long)Integer.parseInt(this.TxtCedula.getText())));
		JOptionPane.showMessageDialog(null,"El Estudiante fué Eliminado con éxito", "Operación Realizada", JOptionPane.INFORMATION_MESSAGE);
	    Borrar();
	}
	
	ActionListener Cancelar = new ActionListener()
	{
		public void actionPerformed(ActionEvent e)
		{
				Cerrar();
		}
    };
	public void Cerrar()
	{
		this.dispose();
	}

	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			getContentPane().setBackground(Color.white);
			this.setTitle("Estudiante");
			{
				BTIncluir = new JButton();
				getContentPane().add(BTIncluir);
				BTIncluir.setText("Incluir");
				BTIncluir.setBounds(24, 212, 79, 29);
				BTIncluir.addActionListener(Incluir);
			}
			{
				BTCancelar = new JButton();
				getContentPane().add(BTCancelar);
				BTCancelar.setText("Salir");
				BTCancelar.setBounds(344, 212, 79, 29);
				BTCancelar.addActionListener(Cancelar);
			}
			{
				BTBorrar = new JButton();
				getContentPane().add(BTBorrar);
				BTBorrar.setText("Borrar");
				BTBorrar.setBounds(230, 212, 80, 29);
				BTBorrar.addActionListener(Borrar);
			}
			{
				jPanel1 = new JPanel();
				getContentPane().add(jPanel1);
				jPanel1.setBounds(34, 50, 372, 139);
				jPanel1.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
				jPanel1.setLayout(null);
				jPanel1.setOpaque(false);
				{
					jLabel8 = new JLabel();
					jPanel1.add(jLabel8);
					jLabel8.setText("Cedula");
					jLabel8.setBounds(25, 28, 64, 15);
					jLabel8.setFont(new java.awt.Font("DejaVu Sans",0,14));
				}
				{
					jLabel6 = new JLabel();
					jPanel1.add(jLabel6);
					jLabel6.setText("Apellido");
					jLabel6.setBounds(23, 100, 79, 15);
					jLabel6.setFont(new java.awt.Font("DejaVu Sans",0,14));
				}
				{
					jLabel7 = new JLabel();
					jPanel1.add(jLabel7);
					jLabel7.setText("Nombre");
					jLabel7.setBounds(23, 62, 79, 15);
					jLabel7.setFont(new java.awt.Font("DejaVu Sans",0,14));
				}
				{
					TxtCedula = new JTextField();
					jPanel1.add(TxtCedula);
					TxtCedula.setBounds(105, 22, 137, 22);
				}
				{
					TxtNombre = new JTextField();
					jPanel1.add(TxtNombre);
					TxtNombre.setBounds(105, 57, 137, 22);
				}
				{
					TxtApellido = new JTextField();
					jPanel1.add(TxtApellido);
					TxtApellido.setBounds(105, 95, 137, 22);
				}
				{
					BTBuscar = new JButton();
					jPanel1.add(BTBuscar);
					BTBuscar.setText("Buscar");
					BTBuscar.setBounds(266, 22, 83, 22);
					BTBuscar.addActionListener(AccBuscar);
				}
			}
			{
				jLabel1 = new JLabel();
				getContentPane().add(jLabel1);
				jLabel1.setText("Datos del Estudiante");
				jLabel1.setBounds(69, 12, 310, 32);
				jLabel1.setFont(new java.awt.Font("Bitstream Charter",1,26));
			}
			{
				BTEliminar = new JButton();
				getContentPane().add(BTEliminar);
				BTEliminar.setText("Eliminar");
				BTEliminar.setBounds(125, 212, 80, 29);
				BTEliminar.addActionListener(Eliminar);
			}

			pack();
			this.setSize(448, 306);
			Borrar();
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}

}
